package com.huibo.aigou.util;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class RedisUtil {
	
	private static class RedisUtilHodler {
		private static JedisPool jedisPool;
		static {
			JedisPoolConfig jpc = new JedisPoolConfig();
			jpc.setMaxIdle(1);
			jpc.setMaxTotal(11);
			jpc.setMaxWaitMillis(10*1000);
			jpc.setTestOnBorrow(true);
			jedisPool = new JedisPool(jpc,"localhost",6379,10*1000, "123321");
		}
	}
	
	private static JedisPool getJedisPool() {
		return RedisUtilHodler.jedisPool;
	}
	
	private static Jedis getJedis() {
		return getJedisPool().getResource();
	}
	
	private static void close(Jedis jedis) {
		if(jedis != null) {
			jedis.close();
		}
	}
	
	public static void set(String key,String value) {
		Jedis jedis = null;
		try {
			jedis = getJedis();
			jedis.set(key, value);
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			close(jedis);
		}
	}
	
	public static String get(String key) {
		Jedis jedis = null;
		String value = null;
		try {
			jedis = getJedis();
			value = jedis.get(key);
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			close(jedis);
		}
		return value;
	}
	
	public static void main(String[] args) {
		//set("name","liwei");
		System.out.println(get("name"));
	}

}












